import unittest, requests


class A3mall(unittest.TestCase):
    # 登录
    token = None

    def test_A3mall_01(self):
        ti = {"username": "18635366586", "password": "123456"}
        r1 = requests.post(url='http://10.59.9.14:7003/api/public/login', json=ti)
        print(r1.json())
        A3mall.token = r1.json()['data']['token']

    # 商品列表
    def test_A3mall_02(self):
        r2 = requests.get(
            url='http://10.59.9.14:7003/api/search/list?page=1&keywords=%E6%B5%8B%E8%AF%95%E6%9C%BA&type=0&sort=1',
            headers={'Auth-Token': 'Bearer ' + A3mall.token})
        print(r2.json())

    # 商品详情
    def test_A3mall_03(self):
        r3 = requests.get(url='http://10.59.9.14:7003/api/goods/view?id=40',
                          headers={'Auth-Token': 'Bearer ' + A3mall.token})
        print(r3.json())

    # 加入购物车
    def test_A3mall_04(self):
        ti = {"id": 40, "sku_id": "", "num": 1}
        r4 = requests.post(url='http://10.59.9.14:7003/api/cart/add', headers={'Auth-Token': 'Bearer ' + A3mall.token},
                           json=ti)
        print(r4.json())

    # 购物车列表
    spid = None

    def test_A3mall_05(self):
        r5 = requests.get(url='http://10.59.9.14:7003/api/cart?page=1',
                          headers={'Auth-Token': 'Bearer ' + A3mall.token})
        print(r5.json())
        A3mall.spid = r5.json()['data']['list'][0]['id']

    # 提交购物车
    def test_A3mall_06(self):
        r6 = requests.get(
            url='http://10.59.9.14:7003/api/order/confirm?id=' + str(A3mall.spid) + '&type=cart&shipping_type=1',
            headers={'Auth-Token': 'Bearer ' + A3mall.token})
        print(r6.json())

    # 支付成功
    msg = None

    def test_A3mall_07(self):
        ti = {"id": str(A3mall.spid), "type": "cart", "address_id": 48, "store_id": 1, "shipping_type": 1,
              "bonus_id": "0",
              "payment": "balance", "remarks": "", "source": 1,
              "url": "http://10.59.9.14:7003/wap/pages/cart/confirm?id=658&type=cart"}
        r7 = requests.post(url='http://10.59.9.14:7003/api/order/create',
                           headers={'Auth-Token': 'Bearer ' + A3mall.token}, json=ti)
        print(r7.json())
        A3mall.msg = r7.json()['data']['msg']
        # self.assertEqual(A3mall.msg, '支付成功')
        # self.assertNotEqual(A3mall.msg, '支付失败')
        self.assertIn('成功', A3mall.msg)
        self.assertNotIn(A3mall.msg, '支付失败')
        # self.assertTrue(A3mall.msg == '支付成功')
